/**
 * 15/02/2014
 * 
 * StateController
 */
package org.cuatrovientos.erp.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.validation.Valid;

import org.apache.log4j.Logger;
import org.cuatrovientos.erp.business.RoleBOInterface;
import org.cuatrovientos.erp.model.Role;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * Controller class form Role management
 * @author Inaki Torres
 */
@Controller
public class RoleController {

	//Get log4j logger
	private static final Logger logger = 
			Logger.getLogger(RoleController.class);

	@Autowired
	private RoleBOInterface roleBO;

	/**
	 * default constructor
	 */
	public RoleController () {
		logger.info("Role BEAN instantiated");
	}

	/**
	 * handles default /role or /role/list request
	 * @return
	 */
	@RequestMapping({"/role","/role/list"})
	public ModelAndView roleDefault() {
		logger.info("default");

		Map<String, Object> model = new HashMap<String, Object>();

		List<Role> roleList = roleBO.selectAll();
		model.put("roles", roleList );
		
		return new ModelAndView("RoleList", model);     
	}


	/**
	 * handles default /role/search request
	 * @param name
	 * @return
	 */
	@RequestMapping(method=RequestMethod.POST,value={"/role/search"})
	public ModelAndView search(
			@RequestParam(value="name", defaultValue="", required=true) 
			String description) {
		
		logger.info("search");

		Map<String, Object> model = new HashMap<String, Object>();

		List<Role> roleList = roleBO.search(description);
		model.put("roles", roleList );
		
		return new ModelAndView("RoleList", model);     
	}

	/**
	 * handles default /role/delete request
	 * @param id
	 * @return
	 */
	@RequestMapping(method=RequestMethod.GET,value={"/role/delete"})
	public String deleteRole(@RequestParam(value="id", defaultValue="", required=true) int id) {
		logger.info("delete " + id);

		roleBO.deleteById(id);
		
		return "redirect:/role";
	}

	/**
	 * handles default /role/new request
	 * @return
	 */
	@RequestMapping(method=RequestMethod.GET,value={"/role/new"})
	public ModelAndView newRole() {
		logger.info("new role");
		
		Role role = new Role();

		return new ModelAndView("NewRole", "role", role);
	}

	/**
	 * handles default /role/save request
	 * @param role
	 * @param bindingResult
	 * @return
	 */
	@RequestMapping(method=RequestMethod.POST,value={"/role/save"})
	public String newRole(@Valid Role role, BindingResult bindingResult) {
		logger.info("new role " + role.toString());

		//In case of validation errors, go back to form
		if (bindingResult.hasErrors()) {
			Map<String, Object> model = new HashMap<String, Object>();
			model.put("role", role);
			
			return "NewRole";
		}

		roleBO.insert(role);
		
		return "redirect:/role";
	}

	/**
	 * handles default /role/detail request
	 * @param id
	 * @return
	 */
	@RequestMapping(method=RequestMethod.GET,value={"/role/detail"})
	public ModelAndView roleDetail(@RequestParam(value="id", defaultValue="", required=true) int id) {
		logger.info("Detail " + id);
		
		Map<String,Object> model = new HashMap<String,Object>();
		Role role = roleBO.selectById(id);
		model.put("role", role);

		return new ModelAndView("RoleDetail", model);
	}


	/**
	 * handles default /role/update request
	 * @param id
	 * @return
	 */
	@RequestMapping(method=RequestMethod.GET,value={"/role/update"})
	public ModelAndView update(@RequestParam(value="id", defaultValue="", required=true) int id) {
		logger.info("update " + id);
		
		Map<String,Object> model = new HashMap<String,Object>();
		Role role = roleBO.selectById(id);
		model.put("role", role);
		
		return new ModelAndView("UpdateRole",model);
	}


	/**
	 * handles default /role/saveupdate request
	 * @param role
	 * @param bindingResult
	 * @return
	 */
	@RequestMapping(method=RequestMethod.POST,value={"/role/saveupdate"})
	public String saveUpdate(@Valid Role role, BindingResult bindingResult) {
		logger.info("update role " + role.toString());

		// In case of validation errors, go back to form
		if (bindingResult.hasErrors()) {
			Map<String, Object> model = new HashMap<String, Object>();
			model.put("role", role);
			
			return "UpdateRole";
		}

		roleBO.update(role);
		
		return "redirect:/role";
	}
	
	/**
	 * @param model
	 * @return
	 */
	@RequestMapping({"/role/index"})
	public String sayHelloPage(Map<String, Object> model) {
		return "index/IndexIssuetype";
	}
	
}
